//////////////////////////////////////////////////////////////////////
//
//	ZETeam source code 
//	Copyright (c) ZETeam 2011-..
//	
//	File: CAngles.cpp
//  Description: Angles classes. Based on Id Software source code.
// 
//	History:
//	- February 27, 2011: Created by Max Streltsov
//
//////////////////////////////////////////////////////////////////////

#include "CAngles.h"
#pragma hdrstop

#include "CVector.h"

#include <float.h>

CAngles ang_zero( 0.0f, 0.0f, 0.0f );

/*
=================
CAngles::Normalize360

returns angles normalized to the range [0 <= angle < 360]
=================
*/
CAngles& CAngles::normalize360( void ) {
	int i;

	for ( i = 0; i < 3; i++ ) {
		if ( ( (*this)[i] >= 360.0f ) || ( (*this)[i] < 0.0f ) ) {
			(*this)[i] -= floor( (*this)[i] / 360.0f ) * 360.0f;

			if ( (*this)[i] >= 360.0f ) {
				(*this)[i] -= 360.0f;
			}
			if ( (*this)[i] < 0.0f ) {
				(*this)[i] += 360.0f;
			}
		}
	}

	return *this;
}

/*
=================
CAngles::Normalize180

returns angles normalized to the range [-180 < angle <= 180]
=================
*/
CAngles& CAngles::normalize180( void ) {
	normalize360();

	if ( pitch > 180.0f ) {
		pitch -= 360.0f;
	}
	
	if ( yaw > 180.0f ) {
		yaw -= 360.0f;
	}

	if ( roll > 180.0f ) {
		roll -= 360.0f;
	}
	return *this;
}

/*
=================
CAngles::ToVectors
=================
*/
void CAngles::toVectors( CVec3 *forward, CVec3 *right, CVec3 *up ) const {
	float sr, sp, sy, cr, cp, cy;
	
	CZMath::SinCos( DEG2RAD( yaw ), sy, cy );
	CZMath::SinCos( DEG2RAD( pitch ), sp, cp );
	CZMath::SinCos( DEG2RAD( roll ), sr, cr );

	if ( forward ) {
		forward->set( cp * cy, cp * sy, -sp );
	}

	if ( right ) {
		right->set( -sr * sp * cy + cr * sy, -sr * sp * sy + -cr * cy, -sr * cp );
	}

	if ( up ) {
		up->set( cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp );
	}
}

/*
=================
CAngles::ToForward
=================
*/
CVec3 CAngles::toForward( void ) const {
	float sp, sy, cp, cy;
	
	CZMath::SinCos( DEG2RAD( yaw ), sy, cy );
	CZMath::SinCos( DEG2RAD( pitch ), sp, cp );

	return CVec3( cp * cy, cp * sy, -sp );
}

/*
=================
CAngles::toQuat
=================
*/
CQuat CAngles::toQuat( void ) const {
	float sx, cx, sy, cy, sz, cz;
	float sxcy, cxcy, sxsy, cxsy;

	CZMath::SinCos( DEG2RAD( yaw ) * 0.5f, sz, cz );
	CZMath::SinCos( DEG2RAD( pitch ) * 0.5f, sy, cy );
	CZMath::SinCos( DEG2RAD( roll ) * 0.5f, sx, cx );

	sxcy = sx * cy;
	cxcy = cx * cy;
	sxsy = sx * sy;
	cxsy = cx * sy;

	return CQuat( cxsy*sz - sxcy*cz, -cxsy*cz - sxcy*sz, sxsy*cz - cxcy*sz, cxcy*cz + sxsy*sz );
}

/*
=================
CAngles::toRotation
=================
*/
CRotation CAngles::toRotation( void ) const {
	CVec3 vec;
	float angle, w;
	float sx, cx, sy, cy, sz, cz;
	float sxcy, cxcy, sxsy, cxsy;

	if ( pitch == 0.0f ) {
		if ( yaw == 0.0f ) {
			return CRotation( vec3_origin, CVec3( -1.0f, 0.0f, 0.0f ), roll );
		}
		if ( roll == 0.0f ) {
			return CRotation( vec3_origin, CVec3( 0.0f, 0.0f, -1.0f ), yaw );
		}
	} else if ( yaw == 0.0f && roll == 0.0f ) {
		return CRotation( vec3_origin, CVec3( 0.0f, -1.0f, 0.0f ), pitch );
	}

	CZMath::SinCos( DEG2RAD( yaw ) * 0.5f, sz, cz );
	CZMath::SinCos( DEG2RAD( pitch ) * 0.5f, sy, cy );
	CZMath::SinCos( DEG2RAD( roll ) * 0.5f, sx, cx );

	sxcy = sx * cy;
	cxcy = cx * cy;
	sxsy = sx * sy;
	cxsy = cx * sy;

	vec.x =  cxsy * sz - sxcy * cz;
	vec.y = -cxsy * cz - sxcy * sz;
	vec.z =  sxsy * cz - cxcy * sz;
	w =		 cxcy * cz + sxsy * sz;
	angle = CZMath::ACos( w );
	if ( angle == 0.0f ) {
		vec.set( 0.0f, 0.0f, 1.0f );
	} else {
		//vec *= (1.0f / sin( angle ));
		vec.normalize();
		vec.fixDegenerateNormal();
		angle *= 2.0f * CZMath::M_RAD2DEG;
	}
	return CRotation( vec3_origin, vec, angle );
}

/*
=================
CAngles::ToMat3
=================
*/
CMat3 CAngles::toMat3( void ) const {
	CMat3 mat;
	float sr, sp, sy, cr, cp, cy;

	CZMath::SinCos( DEG2RAD( yaw ), sy, cy );
	CZMath::SinCos( DEG2RAD( pitch ), sp, cp );
	CZMath::SinCos( DEG2RAD( roll ), sr, cr );

	mat[ 0 ].set( cp * cy, cp * sy, -sp );
	mat[ 1 ].set( sr * sp * cy + cr * -sy, sr * sp * sy + cr * cy, sr * cp );
	mat[ 2 ].set( cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp );

	return mat;
}

/*
=================
CAngles::ToMat4
=================
*/
CMat4 CAngles::toMat4( void ) const {
	return toMat3().toMat4();
}

/*
=================
CAngles::ToAngularVelocity
=================
*/
CVec3 CAngles::toAngularVelocity( void ) const {
	CRotation rotation = CAngles::toRotation();
	return rotation.getVec() * DEG2RAD( rotation.getAngle() );
}

/*
=============
CAngles::ToString
=============
*/
//const char *CAngles::ToString( int precision ) const {
//	return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
//}
